package com.dckeji.shiplock.framework.util;


import java.io.UnsupportedEncodingException;
import java.text.NumberFormat;
import java.util.HashMap;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Created with IntelliJ IDEA.
 * User: TianWei
 * Date: 2017/6/12
 * Time: 12:01
 * To change this template use File | Settings | File Templates.
 */
public class StringUtil {

    public static final String arrTest[] = {"[br]", "[/b]", "[/i]", "[/u]", "[/size]", "[/color]", "[/align]", "[/url]", "[/email]", "[/img]"};
    public static final String arrParam[] = {"\\[br\\]", "\\[b\\](.+?)\\[/b\\]",
            "\\[i\\](.+?)\\[/i\\]",
            "\\[u\\](.+?)\\[/u\\]",
            "\\[size=(.+?)\\](.+?)\\[/size\\]",
            "\\[color=(.+?)\\](.+?)\\[/color\\]",
            "\\[align=(.+?)\\](.+?)\\[/align\\]",
            "\\[url=(.+?)\\](.+?)\\[/url\\]",
            "\\[email=(.+?)\\](.+?)\\[/email\\]," +
                    "\\[img=(.+?)\\](.+?)\\[/img\\]"};
    public static final String arrCode[] = {"<br>", "<b>$1</b>", "<i>$1</i>", "<u>$1</u>",
            "<font size=\"$1\">$2</font>",
            "<font color=\"$1\">$2</font>",
            "<div align=\"$1\">$2</div>",
            "<a href=\"$1\" target=\"_blank\">$2</a>",
            "<a href=\"email:$1\">$2</a>",
            "<img src=\"$1\" border=0>$2</img>"};


    public static int getInt(String content) {
        int intContent;
        try {
            intContent = Integer.parseInt(content);
        } catch (Exception e) {
            intContent = 0;
        }
        return intContent;
    }

    public static long getLong(String content) {
        long lngContent;
        try {
            lngContent = Long.parseLong(content);
        } catch (Exception e) {
            lngContent = 0L;
        }
        return lngContent;
    }

    /**
     * 判断输入的字符串是否满足要求
     *
     * @param str   匹配的字符串
     * @param regex 正则表达式字符串
     * @return
     */
    public static boolean replace(String str, String regex) {
        Pattern pattern = Pattern.compile(regex);
        Matcher matcher = pattern.matcher(str);
        return matcher.matches();
    }

    /**
     * 是否是数值
     *
     * @param str
     * @return
     */
    public static boolean isNumeric(String str) {
        Pattern pattern = Pattern.compile("[0-9]*");
        return pattern.matcher(str).matches();
    }

    /**
     * @param str    原字符串
     * @param length 字符串达到多长才截取
     * @return
     */
    public static String subStringToPoint(String str, int length, String more) {

        String reStr = "";

        if ((null != str) && (str.length() * 2 - 1 > length)) {

            int reInt = 0;


            char[] tempChar = str.toCharArray();

            for (int kk = 0; (kk < tempChar.length && length > reInt); kk++) {

                String s1 = str.valueOf(tempChar[kk]);

                byte[] b = s1.getBytes();

                reInt += b.length;

                reStr += tempChar[kk];

            }

            if (length == reInt || (length == reInt - 1)) {

                if (!reStr.equals(str)) {
                    reStr += more;
                }
            }

        } else {
            reStr = str;
        }
        return reStr;

    }


    /**
     * 将指定的对象转换为String类型
     *
     * @param curObject 传入对象参数
     * @return String
     */
    public static String getString(Object curObject) {
        if (null == curObject) {
            throw new NullPointerException("The input object is null.");
        } else {
            return curObject.toString();
        }
    }

    /**
     * 转换字符,用于替换提交的数据中存在非法数据:"'"
     *
     * @param Content
     * @return
     */
    public static String replaceChar(String content) {
        String newstr = "";
        newstr = content.replaceAll("\'", "''");
        return newstr;
    }

    /**
     * 对标题""转换为中文“”采用对应转换
     *
     * @param Content
     * @return
     */
    public static String replaceSymbol(String content) {
        int intPlaceNum = 0;
        int Num = 0;
        String strContent = content;
        while (true) {
            //判断是否还存在"
            intPlaceNum = strContent.indexOf("\"");
            if (intPlaceNum < 0) {
                break;
            } else {
                if (Num % 2 == 0) {
                    strContent = strContent.replaceFirst("\"", "“");
                } else {
                    strContent = strContent.replaceFirst("\"", "”");
                }
                Num = Num + 1;
            }
        }
        return strContent;
    }

    /**
     * 替换HTML标记
     *
     * @param Content
     * @return
     */
    public static String replaceCharToHtml(String content) {
        String strContent = content;
        strContent = strContent.replaceAll("<", "&lt;");
        strContent = strContent.replaceAll(">", "&gt;");
        strContent = strContent.replaceAll("\"", "&quot;");
        return strContent;
    }

    public static String replaceHtmlToChar(String content) {
        String strContent = content;
        strContent = strContent.replaceAll("&lt;", "<");
        strContent = strContent.replaceAll("&gt;", ">");
        strContent = strContent.replaceAll("&quot;", "\"");
        return strContent;
    }

    //数据库替换
    public static String replaceCharToSql(String content) {
        String strContent = content;
        strContent = strContent.replaceAll("%", "\\\\%");
        return strContent;
    }

    public static String toHtmlValue(String value) {
        if (null == value) {
            return null;
        }
        char a = 0;
        StringBuffer buf = new StringBuffer();
        for (int i = 0; i < value.length(); i++) {
            a = value.charAt(i);
            switch (a) {
                // 双引号
                case 34:
                    buf.append("&#034;");
                    break;
                // &号
                case 38:
                    buf.append("&amp;");
                    break;
                // 单引号
                case 39:
                    buf.append("&#039;");
                    break;
                // 小于号
                case 60:
                    buf.append("&lt;");
                    break;
                // 大于号
                case 62:
                    buf.append("&gt;");
                    break;
                default:
                    buf.append(a);
                    break;
            }
        }
        return buf.toString();
    }


    /**
     * 标题中含有特殊字符替换 如:●▲@◎※ 主要在标题中使用
     *
     * @param Content
     * @return
     */
    public static String replaceSign(String content) {
        String strContent = "";
        strContent = content.replaceAll("\\*", "");
        strContent = strContent.replaceAll("\\$", "");
        strContent = strContent.replaceAll("\\+", "");
        String arrStr[] = {":", "：", "●", "▲", "■", "@", "＠",
                "◎", "★", "※", "＃", "〓", "＼", "§", "☆",
                "○", "◇", "◆", "□", "△", "＆", "＾", "￣",
                "＿", "♂", "♀", "Ю", "┭", "①", "「", "」", "≮", "§",
                "￡", "∑", "『", "』", "⊙", "∷", "Θ", "の", "↓", "↑",
                "Ф", "~", "Ⅱ", "∈", "┣", "┫", "╋", "┇", "┋", "→",
                "←", "!", "Ж", "#"};
        for (int i = 0; i < arrStr.length; i++) {
            if ((strContent.indexOf(arrStr[i])) >= 0) {
                strContent = strContent.replaceAll(arrStr[i], "");
            }
        }

        return strContent;
    }

    /**
     * 替换所有英文字母
     *
     * @param Content
     * @return
     */
    public static String replaceLetter(String content) {
        String strMark = "[^[A-Za-z]+$]";
        String strContent = "";
        strContent = content.replaceAll(strMark, "");
        return strContent;
    }

    /**
     * 替换所有数字
     *
     * @param Content
     * @return
     */
    public static String replaceNumber(String content) {
        String strMark = "[^[0-9]+$]";
        String strContent = "";
        strContent = content.replaceAll(strMark, "");
        return strContent;
    }

    /**
     * 将/n转换成为回车<br> ,空格转为&nbsp;
     *
     * @param Content
     * @return
     */
    public static String replaceBr(String content) {
        if (content == null) {
            return "";
        }
        String strContent = "";

        // String strMark ="[/\n\r\t]";

        //strContent = content.replaceAll(strMark,"<br>");

        strContent = content.replaceAll("\n\r\t", "<br>");
        strContent = strContent.replaceAll("\n\r", "<br>");
        strContent = strContent.replaceAll("\r\n", "<br>");
        strContent = strContent.replaceAll("\n", "<br>");
        strContent = strContent.replaceAll("\r", "<br>");
        strContent = strContent.replaceAll(" ", "&nbsp;");
        return strContent;
    }

    /**
     * 清除所有<>标记符号 主要在搜索中显示文字内容 而不显示样式
     *
     * @param Content
     * @return
     */
    public static String replaceMark(String content) {
        String strContent = "";
        String strMark = "<\\s*[^>]*>";
        strContent = content.trim();
        strContent = strContent.replaceAll("\"", "");
        strContent = strContent.replaceAll("\'", "");
        //删除所有<>标记
        strContent = strContent.replaceAll(strMark, "");
        strContent = strContent.replaceAll("&nbsp;", "");
        strContent = strContent.replaceAll(" ", "");
        strContent = strContent.replaceAll("　", "");
        strContent = strContent.replaceAll("\r", "");
        strContent = strContent.replaceAll("\n", "");
        strContent = strContent.replaceAll("\r\n", "");
        return strContent;
    }

    /**
     * 清楚WOrd垃圾代码
     *
     * @param Content
     * @return
     */
    public static String clearWord(String content) {
        String strContent = "";
        strContent = content.trim();
        strContent = strContent.replaceAll("x:str", "");
        //Remove Style attributes
        strContent = strContent.replaceAll("<(\\w[^>]*) style=\"([^\"]*)\"", "<$1");
        //Remove all SPAN  tags
        strContent = strContent.replaceAll("<\\/?SPAN[^>]*>", "");
        //Remove Lang attributes
        strContent = strContent.replaceAll("<(\\w[^>]*) lang=([^ |>]*)([^>]*)", "<$1$3");
        //Remove Class attributes
        strContent = strContent.replaceAll("<(\\w[^>]*) class=([^ |>]*)([^>]*)", "<$1$3");
        //Remove XML elements and declarations
        strContent = strContent.replaceAll("<\\\\?\\?xml[^>]*>", "");
        //Remove Tags with XML namespace declarations: <o:p></o:p>
        strContent = strContent.replaceAll("<\\/?\\w+:[^>]*>", "");
        return strContent;
    }

    /**
     * 对组ID信息进行处理 转换为标准ID组 并过滤重复的信息
     *
     * @param teamId
     * @return
     */
    public static String checkTeamId(String teamId) {
        String strTeamId = "";
        String strTempId = "";
        String strTemp = "";
        String[] arrTeamId = teamId.split(",");
        for (int num = 0; num < arrTeamId.length; num++) {
            strTemp = arrTeamId[num].trim();
            if ((!strTemp.equals("")) && (!strTemp.equals("0"))) {
                if ((strTempId.indexOf("," + strTemp + ",")) >= 0) { //表示已经保存过了
                } else {
                    if (strTeamId.equals("")) {
                        strTeamId = strTemp;
                        strTempId = strTempId + "," + strTemp + ",";
                        ;
                    } else {
                        strTeamId = strTeamId + "," + strTemp;
                        strTempId = strTempId + strTemp + ",";
                    }
                }
            }
        }
        return strTeamId;
    }


    public static String replaceUbb(String content) {
        String strContent = content;
        try {
            for (int num = 0; num < arrTest.length; num++) {
                if ((strContent.indexOf(arrTest[num])) >= 0) {
                    try {
                        strContent = strContent.replaceAll(arrParam[num], arrCode[num]);
                    } catch (Exception ex) {
                    }
                }
            }
        } catch (Exception e) {
            //System.out.println("UBB CODE 错误"+e);
        }
        return strContent;
    }


    /**
     * 判断传入的字符串如果为null则返回"",否则返回其本身
     *
     * @param string
     * @param instant
     * @return String
     */
    public static String convertNull(String string, String instant) {
        return isNull(string) ? instant : string;
    }

    /**
     * {@link #convertNull(String, String)}
     *
     * @param string
     * @return String
     */
    public static String convertNull(String string) {
        return convertNull(string, "");
    }

    /**
     * 判断对象是否为空
     *
     * @param obj Object
     * @return boolean 空返回true,非空返回false
     */
    public static boolean isNull(Object obj) {
        return (null == obj) ? true : false;
    }

    /**
     * Description:判断字段空null <br>
     *
     * @param s
     * @return boolean
     */
    public static boolean isNull(String s) {
        if (s == null || "".equals(s.trim())) {
            return true;
        }

        return false;
    }


    public static boolean isFullNull(String s) {
        if (s == null || "".equals(s.trim()) || "null".equals(s)) {
            return true;
        }

        return false;
    }

    /**
     * 获取百分比
     *
     * @param p1
     * @param p2
     * @return
     */
    public static String percent(double p1, double p2) {
        if (p2 == 0) {
            return "0.00%";
        }
        String str;
        double p3 = p1 / p2;
        NumberFormat nf = NumberFormat.getPercentInstance();
        nf.setMinimumFractionDigits(2);
        str = nf.format(p3);
        return str;
    }

    /**
     * 字符串编码转换的实现方法
     *
     * @param str        待转换编码的字符串
     * @param oldCharset 原编码
     * @param newCharset 目标编码
     * @return
     * @throws UnsupportedEncodingException
     */
    public static String changeCharset(String str, String oldCharset, String newCharset) {
        try {
            if (str != null) {
                //用旧的字符编码解码字符串。解码可能会出现异常。
                byte[] bs = str.getBytes(oldCharset);
                //用新的字符编码生成字符串
                return new String(bs, newCharset);
            }
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
            return "";
        }
        return "";
    }

    /**
     * 字符串编码转换的实现方法
     *
     * @param str        待转换编码的字符串
     * @param newCharset 目标编码
     * @return
     * @throws UnsupportedEncodingException
     */
    public String changeCharset(String str, String newCharset) {
        try {
            if (str != null) {
                //用默认字符编码解码字符串。
                byte[] bs = str.getBytes();
                //用新的字符编码生成字符串
                return new String(bs, newCharset);
            }
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return "";
    }

    /**
     * 解析html中的参数信息
     *
     * @param elementStr
     * @return
     */
    public static Map<String, String> getConfigValue(String elementStr) {
        try {
            elementStr = java.net.URLDecoder.decode(elementStr, "utf-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        int start = elementStr.indexOf("configvalue");
        Map<String, String> map = null; //参数的键值对
        if (start != -1) {
            map = new HashMap<String, String>();
            start = elementStr.indexOf("\"", start);
            int end = elementStr.lastIndexOf("||");
            if (start < 0 || end < 0) {
                return null;
            }
            String configValue = elementStr.substring(start + 1, end);
            String[] values = configValue.split("\\|\\|");

            for (int i = 0; i < values.length; i++) {
                String value = values[i];
                if (value != null && value.trim().length() > 1) {
                    int de = value.indexOf("=");
                    if (de > 0) {
                        String name = value.substring(0, de);
                        String v = value.substring(de + 1);
                        map.put(name, v);
                    }
                }
            }
        }
        return map;
    }

    /**
     * 转换空值为0
     *
     * @param str
     * @return
     */
    public static String conventString(String str) {
        return null == str || "".equals(str) ? "" + "0" : str;
    }

    public static void main(String[] args) {
        Pattern pattern = Pattern.compile("<span\\s.+?]]</span>");
        String str = "<span configvalue=\"eid=1043||ename=%E9%9D%A2%E5%8C%85%E5%B1%91||folderId=CLBLYM||\" contenteditable=\"false\" style=\"background-color: #ffff00; color: #000000\">[[ 面包屑 ]]</span><br />"
                + "业务名称：<span configvalue=\"eid=1042||ename=%E4%B8%9A%E5%8A%A1%E5%90%8D%E7%A7%B0||busiNum=CL||\" contenteditable=\"false\" style=\"background-color: #ffff00; color: #000000\">[[ 业务名称 ]]</span><br />"
                + "业务资费：<span configvalue=\"eid=1041||ename=%E4%B8%9A%E5%8A%A1%E8%B5%84%E8%B4%B9||busiNum=CL||\" contenteditable=\"false\" style=\"background-color: #ffff00; color: #000000\">[[ 业务资费 ]]</span><br />"
                + "业务介绍：<br />"
                + "<span configvalue=\"eid=1040||ename=%E4%B8%9A%E5%8A%A1%E4%BB%8B%E7%BB%8D||busiNum=CL||\" contenteditable=\"false\" style=\"background-color: #ffff00; color: #000000\">[[ 业务介绍 ]]</span><br />"
                + "<br />"
                + "<br />"
                + "<br /><span >]]</span>"
                + "<span configvalue=\"eid=1043||ename=%E9%9D%A2%E5%8C%85%E5%B1%91||folderId=CLBLYM||\" contenteditable=\"false\" style=\"background-color: #ffff00; color: #000000\">[[ 面包屑 ]]</span><br />";
        Matcher matcher = pattern.matcher(str);
        String htmlStr = "";
        StringBuffer strbuff = new StringBuffer();

        int i = 0;
        while (matcher.find()) {
            String bm = matcher.group();
            System.out.println(bm);
            Map map = getConfigValue(bm);
            if (map != null) {
                //todo:从缓存中获取数据
                if (((String) map.get("eid")).equals("1043")) {
                    String se = "【掌上营业厅】+ 1";
                    matcher.appendReplacement(strbuff, Matcher.quoteReplacement(se == null ? "" : se));
                } else if (((String) map.get("eid")).equals("1042")) {
                    String se = "【掌上营业厅】+ 2";
                    matcher.appendReplacement(strbuff, Matcher.quoteReplacement(se == null ? "" : se));
                } else if (((String) map.get("eid")).equals("1041")) {
                    String se = "【掌上营业厅】 + 3";
                    matcher.appendReplacement(strbuff, Matcher.quoteReplacement(se == null ? "" : se));
                } else if (((String) map.get("eid")).equals("1040")) {
                    String se = "【掌上营业厅】+ 4";
                    matcher.appendReplacement(strbuff, Matcher.quoteReplacement(se == null ? "" : se));
                } else if (((String) map.get("eid")).equals("1046")) {
                    String se = "【掌上营业厅】+888888888888";
                    matcher.appendReplacement(strbuff, Matcher.quoteReplacement(se == null ? "" : se));
                }
                i++;
            }
        }
        matcher.appendTail(strbuff);
        htmlStr += strbuff.toString();
        System.out.println(htmlStr + "=================" + i);
    }

    /**
     * 方法描述：将传入的字符串转换成整型数据，如果转换过程中发生异常，则返回默认值：defaultValue。
     * 创建日期：2013-12-7下午11:40:20
     * 修改日期：
     * 作者：zhanglu
     *
     * @param:
     * @return:int
     */
    public static int convertIntoInt(String str, int defaultValue) {
        // 定义一个返回值，假如转型过程中发生异常，则返回此默认值
        int retData = defaultValue;
        try {
            retData = Integer.parseInt(str.trim());
        } catch (NumberFormatException e) {
        } catch (Exception ex) {
        }

        return retData;
    }

    /**
     * 适用于服务端对客户端版本判断
     *
     * @param str1 前一个版本
     * @param str2 后一个版本
     * @return 小于0, 前者小于后者 ; 0:相等 ;大于0,前者大于后者
     * @author NZF 2014年12月9日
     */
    public static int compareTwoStr(String str1, String str2) {
        str1 = str1 == null ? "" : str1;
        str2 = str2 == null ? "" : str2;
        if (StringUtil.isFullNull(str1) && !StringUtil.isFullNull(str2)) {
            return -1;
        } else if (!StringUtil.isFullNull(str1) && StringUtil.isFullNull(str2)) {
            return 1;
        } else if (str1.equals(str2)) {
            return 0;
        } else {
            return str1.compareTo(str2);
        }
    }

    /**
     * 适用于服务端判断版本升级,比较 一个数是否在区间返回内
     *
     * @param str1 需比较的参数,不能为空,如果为空,则默认返回不再区间内
     * @param str2 区间下限
     * @param str3 区间上限
     * @return 1:无需升级(等于区间上限值);2:建议升级(不包括区间的两个极限值);3:强制升级(包括区间下限值)
     * @author NZF 2014年12月9日
     */
    public static int compareRangeStr(String str1, String str2, String str3) {
        str1 = str1 == null ? "" : str1;
        str2 = str2 == null ? "" : str2;
        str3 = str3 == null ? "" : str3;
        //当前版本号如果为空,即str1为空,则默认无需升级
        if (StringUtil.isFullNull(str1)) {
            return 1;
        } else {
            //当前版本大于等于区间上限,则是无需升级
            if (compareTwoStr(str1, str3) >= 0) {
                return 1;
            }
            //当前版本小于等于区间下限,则是强制升级
            if (compareTwoStr(str1, str2) <= 0) {
                return 3;
            }
            //其他情况都是建议升级
            return 2;
        }
    }

    public static String stringLimit(String s, int len) {
        if (!isFullNull(s)) {
            byte[] b;
            try {
                b = s.getBytes("UTF-8");
                int res = b.length;
                if (res > len) {
                    return new String(b, 0, len - 1);
                } else {
                    return s;
                }
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            }

        }
        return "";
    }

    /**
     * 用于区分判断各个渠道(入参1.PageContext 2.要判断的渠道)
     * 入参1传PageContext用户获取platForm和appchannel
     * 入参2传要判断的渠道 返回true or false    例如: 标版 传android,返回true,TV版 传tv,返回true
     * Android = android
     * Iphone  = iphone
     * 娱乐版 = entertain
     * TV版  = tv
     * 其他返回false
     *
     * @param args
     */
    public static boolean queryPlatForm(String platForm, String channel, String platF) {
        String key = platForm;
        if (!StringUtil.isFullNull(channel)) {
            key += "_" + channel;
        }
        if (!StringUtil.isFullNull(platF)) {
            if ("android".equalsIgnoreCase(key) && platF.trim().equalsIgnoreCase("android")) {
                return true;
            } else if ("iphone".equalsIgnoreCase(key) && platF.trim().equalsIgnoreCase("iphone")) {
                return true;
            } else if ("android_entertain".equalsIgnoreCase(key) && platF.trim().equalsIgnoreCase("entertain")) {
                return true;
            } else if ("android_tv".equalsIgnoreCase(key) && platF.trim().equalsIgnoreCase("tv")) {
                return true;
            }
        }
        return false;
    }

    public static boolean isFullAllNull(String s) {
        if (s == null || "".equals(s.trim()) || "null".equals(s) || "(null)".equals(s)) {
            return true;
        } else {
            return false;
        }
    }

    /**
     * 适用于服务端对客户端版本判断
     *
     * @param str1 前一个版本
     * @param str2 后一个版本
     * @return 小于0, 前者小于后者 ; 0:相等 ;大于0,前者大于后者
     */
    public static int compareTwoStrNew(String str1, String str2) {
        str1 = str1 == null ? "" : str1;
        str2 = str2 == null ? "" : str2;
        if (StringUtil.isFullNull(str1) && !StringUtil.isFullNull(str2)) {
            return -1;
        } else if (!StringUtil.isFullNull(str1) && StringUtil.isFullNull(str2)) {
            return 1;
        } else if (str1.equals(str2)) {
            return 0;
        } else {
            String[] str1s = str1.split("\\.");
            String[] str2s = str2.split("\\.");
            int length = str1s.length > str2s.length ? str2s.length : str1s.length;
            for (int i = 0; i < length; i++) {
                if (str1s[i].compareTo(str2s[i]) != 0) {
                    if (Integer.parseInt(str1s[i]) > Integer.parseInt(str2s[i])) {
                        return 1;
                    } else {
                        return -1;
                    }
                } else if (i == length - 1) {
                    return str1s.length > str2s.length ? 1 : -1;
                }
            }
            return -1;
        }
    }
}
